package com.six.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.pojo.Agreement;
import com.six.pojo.vo.AgreementVo;
import com.six.result.Result;
import com.six.result.ResultUtils;
import com.six.service.AgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/agreement")
@Api(tags = "y合同相关接口")
@CrossOrigin
@ResponseBody
public class AgreementController {

    @Autowired
    private AgreementService agreementService;

    /**
     * 根据员工返回他签订的所有合同
     * @param agreementVo
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询员工所有合同")
    public Result list(@RequestBody AgreementVo agreementVo){

        LambdaQueryWrapper<Agreement> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Agreement::getAdminId,agreementVo.getAdminId());
        //分页条件
        Page page = new Page(agreementVo.getPage(),agreementVo.getLimit());

        Page page1 = agreementService.page(page, wrapper);

        return ResultUtils.buildSuccess(page1.getTotal(),page1.getRecords());
    }

    /**
     * 增加合同
     * @param agreement
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加合同")
    public Result add(@RequestBody Agreement agreement){
        agreement.setStartTime(new Date());
        agreementService.save(agreement);
        return ResultUtils.buildSuccess();
    }

    /**
     * 根据合同id修改合同
     * @param agreement
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("根据合同id修改合同")
    public Result update(@RequestBody Agreement agreement){
        agreementService.updateById(agreement);
        return ResultUtils.buildSuccess();
    }


    /**
     * 根据合同id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据合同id删除")
    public Result delete(@PathVariable("id") Long id){
        agreementService.removeById(id);
        return ResultUtils.buildSuccess();
    }
}
